package homework1;

import java.util.Random;
import java.util.Scanner;

public class Maze {
    private int[][] maze;
    private int playerX;          //玩家坐标
    private int playerY;
    private int exitX;
    private int exitY;
    private Random random;
    private Role player;

    public Maze() {
        random = new Random();
        player = new Role();
        player.setBlood(100);
    }

    public void InitMaze() {
        maze = GenerateMaze(8, 8);
        playerX = 0;
        playerY = 0;
        exitX = maze.length - 1;
        exitY = maze[0].length - 1;
        maze[playerX][playerY] = 0; // 入口必须能进
        maze[exitX][exitY] = 0;     // 出口必须能出
    }

    public int[][] GenerateMaze(int rows, int cols) {
        int[][] maze = new int[rows][cols];
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                double rand = random.nextDouble();
                if (rand < 0.2) {
                    maze[i][j] = 1; // 20% chance for obstacle
                } else if (rand < 0.4) {
                    maze[i][j] = 2; // 20% chance for monster
                } else if (rand < 0.7) {
                    maze[i][j] = 3; // 30% chance for potion
                } else if (rand < 0.85) {
                    maze[i][j] = 4; // 15% chance for weapon
                } else if (rand < 1.0) {
                    maze[i][j] = 5; // 15% chance for armor
                } else {
                    maze[i][j] = 0; // Empty space
                }
            }
        }
        return maze;
    }

    public void printMaze() {
        for (int i = 0; i < maze.length; i++) {
            for (int j = 0; j < maze[i].length; j++) {
                if (i == playerX && j == playerY) {
                    System.out.print("★ ");
                } else {
                    switch (maze[i][j]) {
                        case 0: 
                        	System.out.print(". "); break; // Empty
                        case 1: 
                        	System.out.print("█ "); break; // Obstacle
                        case 2: 
                        	System.out.print("M "); break; // Monster
                        case 3: 
                        	System.out.print("P "); break; // Potion
                        case 4: 
                        	System.out.print("W "); break; // Weapon
                        case 5: 
                        	System.out.print("A "); break; // Armor
                        default: 
                        	System.out.print(". ");
                    }
                }
            }
            System.out.println();
        }
    }

    public void movePlayer(char direction) {
        int newX = playerX;
        int newY = playerY;
        
        switch (direction) {
            case 'w': newX--; break;
            case 'a': newY--; break;
            case 's': newX++; break;
            case 'd': newY++; break;
        }
        
        if (isValidMove(newX, newY)) {
            playerX = newX;
            playerY = newY;
            handleCellEvent(newX, newY);
        } else {
            System.out.println("不是哥们，不能走到这里!");
        }
    }
    
    private boolean isValidMove(int x, int y) {
        if (x < 0 || x >= maze.length || y < 0 || y >= maze[0].length) {
            return false;
        }
        return maze[x][y] != 1; 
    }
    
    private void handleCellEvent(int x, int y) {
        switch (maze[x][y]) {
            case 2: // Monster
                fightMonster();
                break;
            case 3: // Potion
                int heal = random.nextInt(20) + 10;
                player.setBlood(player.getBlood() + heal);
                System.out.println("找到了药水! 增加了 " + heal + " HP.");
                maze[x][y] = 0;
                break;
            case 4: // Weapon
                Weapon newWeapon = generateRandomWeapon();
                System.out.println("找到了武器！ " + newWeapon.getName() + " 伤害: " + newWeapon.getHurt() + ")");
                player.setWp(newWeapon);
                maze[x][y] = 0;
                break;
            case 5: // Armor
                Defense newArmor = generateRandomArmor();
                System.out.println("找到了装备: " + newArmor.getName() + " 防御值: " + newArmor.getProtect() + ")");
                player.wear(newArmor);
                maze[x][y] = 0;
                break;
        }
    }
    
    private void fightMonster() {
        System.out.println("遇见了怪物! 开始战斗中...");
        int monsterHealth = random.nextInt(30) + 20;
        int monsterAttack = random.nextInt(10) + 5;
        
        while (monsterHealth > 0 && player.getBlood() > 0) {
            System.out.println("\n玩家 HP: " + player.getBlood());
            System.out.println("怪物 HP: " + monsterHealth);
            System.out.println("选择行动: 1. 物理攻击 2. 魔法攻击");
            
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            
            if (choice == 1) {
                if (player.getWp() != null) {
                    int damage = player.getWp().getHurt() / 2 + random.nextInt(player.getWp().getHurt() / 2);
                    monsterHealth -= damage;
                    System.out.println("你的攻击 " + player.getWp().getName() + " 造成了 " + damage + " 点伤害!");
                } else {
                    int damage = random.nextInt(5) + 3;
                    monsterHealth -= damage;
                    System.out.println("你对怪物造成了" + damage + " 点伤害!");
                }
            } else if (choice == 2) {
                player.faShuSkillAttack();
                int spellDamage = random.nextInt(15) + 10;
                monsterHealth -= spellDamage;
                System.out.println("你的攻击造成了 " + spellDamage + " 点伤害!");
            }
            
            if (monsterHealth > 0) {
                int damage = monsterAttack - (player.getAr() != null ? player.getAr().getProtect() / 10 : 0);
                if (damage < 0) damage = 0;
                player.setBlood(player.getBlood() - damage);
                System.out.println("怪物对你造成了 " + damage + " 点伤害!");
            }
        }
        
        if (monsterHealth <= 0) {
            System.out.println("你战胜了怪物!");
        } else {
            System.out.println("你被怪物打败了...");
        }
    }
    
    private Weapon generateRandomWeapon() {
        String[] names = {"Sword", "Axe", "Mace", "Dagger", "Bow"};
        int[] damages = {15, 20, 25, 10, 18};
        int index = random.nextInt(names.length);
        return new Weapon(names[index], damages[index]);
    }
    
    private Defense generateRandomArmor() {
        String[] names = {"Leather Armor", "Chainmail", "Plate Armor", "Robe", "Shield"};
        int[] protections = {5, 10, 15, 3, 8};
        int index = random.nextInt(names.length);
        return new Defense(names[index], protections[index]);
    }
    
    public boolean checkWin() {
        return playerX == exitX && playerY == exitY;
    }
}